package com.Classroom.al.controllers;

import com.Classroom.al.dao.UserAccountRepository;
import com.Classroom.al.entities.UserAccount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class SecurityController {

    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    UserAccountRepository userAccountRepository;


    @RequestMapping({"/","/home"})
    public String home(){return home();}

    @RequestMapping("/toLogin")
    public String toLogin(){return "/security/login";}


    @GetMapping("/register")
    public String register(Model model){

        UserAccount userAccount = new UserAccount();
        model.addAttribute("userAccount", userAccount);

        return "/security/register1";
    }

    @PostMapping("/register/save")
    public String saveUser(Model model, UserAccount userAccount){
        userAccount.setPassword(bCryptPasswordEncoder.encode(userAccount.getPassword()));

        userAccountRepository.save(userAccount);

        return "redirect:/";
    }
}
